import { log } from "console";
import { Context, Next } from "koa";
import { verify } from "../util/webToken";
// import client from "../util/redis";
const { promisify } = require("util");
// const hgetllAsync = promisify(client.get).bind(client);
/*
 * @Author: fishroom
 * @Date: 2022-09-30 15:53:03
 * @LastEditTime: 2023-01-06 18:27:12
 * @FilePath: \koa\app\middleware\JwtAothMiddleWare.ts
 */
async function JwtAothMiddleWare(ctx: Context, next: Next) {
  let token: any = ctx.headers["token"];
  if (!token) {
    token = ctx.query["token"] || undefined;
  }
  if (token != undefined && token != "") {
    let { admin, error } = verify(token);
    ctx.$user = admin;
    if (error != null) {
      return (ctx.body = {
        message: error,
        code: 4001,
      });
    } else {
      return next();
      // const result = await hgetllAsync(admin);
      // if (result) {

      // }
      // return (ctx.body = {
      //   message: "token过期",
      //   code: 4001,
      // });
    }
  } else {
    return (ctx.body = {
      code: 4000,
      message: "没有token",
    });
  }
}
export default JwtAothMiddleWare;
